module.exports=class extends baseLogic{
    constructor(){
        super();
    }

   static init(){
        return new this();
    }

    async getItems(queryModel){
        let ret = await this.DB.iframe_partners_dict.Access.findAll({
            where: {
                zd_label: queryModel.label,
                zd_partners_id:queryModel.PartnersID
            },
            order: [
                ['zd_sort', 'ASC']
            ],
            raw:true
        });
        return ret;
    }

    async insertOrUpdate(data){
        let Model = {};
        let DictID = data.DictID ? data.DictID : 0;
        let parentID = data.ParentID ? data.ParentID : 0;
        if (DictID > 0) {
            Model = await this.DB.iframe_partners_dict.Access.findById(DictID);
            if (!Model) {
                return null;
            }
            Model = Model.dataValues;
        } else {
            var MaxSort = await this.DB.iframe_partners_dict.Access.max("zd_sort", {
                where: {
                    zd_ParentID: parentID
                }
            });
            if (isNaN(MaxSort)) {
                MaxSort = 0;
            }
            //Model.zd_ID=0;
            Model.IsParent = 0;
            Model.zd_ParentID = parentID;
            Model.zd_sort = Number("0" + MaxSort) + 1;
            // Model.OS_CreateTime = moment().format("YYYY-MM-DD HH:mm:ss");
        }
        Model.zd_label = data.Label;
        Model.zd_Name = data.Name;
        Model.zd_value = data.Value;
        Model.zd_ParentID = parentID;
        Model.zd_Attr = data.Attr;
        Model.zd_IsShow =data.IsShowInMenu ? 1 : 0;
        Model.zd_partners_id=data.zd_partners_id;
        if (DictID > 0) {
            await this.DB.iframe_partners_dict.Access.update(Model, {
                where: {
                    zd_ID: DictID
                }
            });
        } else {
            // Model.zd_ID = await DB.iframe_tabelid.GetID("iframe_partners_dict");
            Model =  await this.DB.iframe_partners_dict.Access.create(Model);
        }
        await this.CheckIsParent_iframe_dict(Model.zd_ID);
        return Model;
    }

    /**
     * 删除
     * @param {*} CurNodeParentID 当前节点的父级ID
     * @param {*} AllIDs_Array 待删除的节点ID数组
     */
    async delModule(CurNodeParentID,AllIDs_Array){
        for (var i = 0; i < AllIDs_Array.length; i++) {
            let id = Number("0" + AllIDs_Array[i]);
            if (id > 0) {
                await this.DB.iframe_partners_dict.Access.destroy({
                    where: {
                        zd_ID: id
                    }
                });
            }
        }
        await this.CheckIsParent_iframe_dict(CurNodeParentID);
        return true;
    }

    async moveNode(queryModel){
        if (queryModel.CurNodeID > 0) {
            await this.DB.iframe_partners_dict.Access.update({
                zd_ParentID: queryModel.TargetParentID
            }, {
                where: {
                    zd_ID: queryModel.CurNodeID
                }
            });
        };

        
        for (let i = 0; i < queryModel.TargetNodeChildrensIDs_array.length; i++) {
            let id = queryModel.TargetNodeChildrensIDs_array[i];
            await this.DB.iframe_partners_dict.Access.update({
                zd_sort: i
            }, {
                where: {
                    zd_ID: id
                }
            });
        };

        await this.CheckIsParent_iframe_dict(queryModel.TargetParentID);
        await this.CheckIsParent_iframe_dict(queryModel.old_parent);
        return true;

    }

    async asycDict(){
       let ret = await this.func.iframeDict.Sync();
       return ret;
    }

    async getdictsTreeData(queryModel){
        let treeData =await this.func.partnersDict.GetTreeData(queryModel.partnersId,queryModel.label,queryModel.parentId);
        return treeData;
    }

    /**
     * 
     * 检测是否是父级
     * @param {*} ParentID 
     */
    async CheckIsParent_iframe_dict(ParentID){
        if (ParentID === 0) {
            return false;
        };
        let count = await this.DB.iframe_partners_dict.Access.count({
            where: {
                zd_ParentID: ParentID
            }
          
        });
        if (count == 0) {
            await this.DB.iframe_partners_dict.Access.update({
                zd_IsParent: 0
            }, {
                where: {
                    zd_ID: ParentID
                }
            });
        } else {
            await this.DB.iframe_partners_dict.Access.update({
                zd_IsParent: 1
            }, {
                where: {
                    zd_ID: ParentID
                }
            });
        }
        return true;
    
    }
}